package weaver.ihg.util;

import com.engine.common.util.ServiceUtil;
import com.engine.hrm.service.RolesMembersService;
import com.engine.hrm.service.impl.RolesMembersServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.hrm.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wujinghe
 * @version 1.0
 * @createTime: 2024/11/22 10:33
 * @description: 角色设置 操作相关工具类
 */

public class RolesMembersUtil {
    private final Logger logger = LoggerFactory.getLogger(RolesMembersUtil.class);

    private static RolesMembersService getRolesMembersService(User var1) {
        return (RolesMembersServiceImpl) ServiceUtil.getService(RolesMembersServiceImpl.class, var1);
    }


    /**
     * 保存角色设置对应角色的成员
     * @param roleId 角色id
     * @param ids 要保存的id列表
     * @param resourceType 成员类型 （该方法暂不支持需要安全级别的类型）
     * @param roleLevel
     * @param user
     */
    public static Map<String, Object> saveRoleMembers(String roleId, List<String> ids, ResourceType resourceType, RoleLevel roleLevel, User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("roleId", roleId);
        map.put("resourcetype", resourceType.ordinal() + 1);
        switch (resourceType) {
            case HRM:
                map.put("resourceid", String.join(",", ids));
                break;
//            case SUB_DEPT:
//                break;
//            case DEPT:
//                break;
//            case JOB:
//                break;
        }

        map.put("rolelevel", roleLevel.ordinal());
        map.put("cmd", "addRolesMembers");
        return getRolesMembersService(user).saveRolesMembers(map, user);
    }

    /**
     * 删除角色设置对应角色的成员
     * todo 未完成 （目前仅支持添加角色）
     * @param roleId 角色id
     * @param ids 要删除的id列表
     * @param user
     */
    public static Map<String, Object> deleteRoleMembers(String roleId, List<String> ids, User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("roleId", roleId);
//        map.put("targetName", "分区管理员");
        map.put("id", String.join(",", ids));
        Map<String, Object> map1 = getRolesMembersService(user).delRolesMembers(map, user);
        return map1;
    }

    /**
     * 成员类型
     */
    public enum ResourceType {
        HRM, SUB_DEPT, DEPT, JOB,//todo 待补全
    }

    /**
     * 成员级别
     */
    public enum RoleLevel {
        DEPT/*部门*/, SUB_DEPT/*分部*/, HEADQUARTERS/*总部*/
    }
}
